package com.charmzenus.champion.ui.activity;

import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.charmzenus.champion.R;
import com.charmzenus.champion.base.BaseActivity;
import com.charmzenus.champion.dagger.component.activity.DaggerMainActivityComponent;
import com.charmzenus.champion.dagger.component.activity.MainActivityComponent;
import com.charmzenus.champion.dagger.module.activity.MainActivityModule;
import com.charmzenus.champion.presenter.activity.MainActivityPresenter;
import com.charmzenus.champion.ui.fragment.main.MainOpenBill;
import com.charmzenus.champion.ui.fragment.main.MainMine;
import com.charmzenus.champion.ui.fragment.main.MainOrder;
import com.charmzenus.champion.ui.view.Header;
import com.charmzenus.champion.utils.FragmentUtils;
import com.charmzenus.champion.utils.ToastUtil;

import javax.inject.Inject;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    @Inject
    MainActivityPresenter presenter;

    private long exitTime = 0;

    private FragmentManager fm;
    private MainOpenBill fragmentOne;
    private MainOrder fragmentTwo;
    private MainMine fragmentThree;
    private RadioGroup main_rg;
    private RadioButton tab1_rb;
    private RadioButton tab2_rb;
    private RadioButton tab3_rb;
    private Header mainTitle;


    @Override
    protected int layoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {

        MainActivityComponent component = DaggerMainActivityComponent
                .builder()
                .mainActivityModule(new MainActivityModule(this))
                .build();
        component.in(this);

        mainTitle = findViewById(R.id.main_title);
        main_rg = findViewById(R.id.main_rg);
        tab1_rb = findViewById(R.id.main_tab1_rb);
        tab2_rb = findViewById(R.id.main_tab2_rb);
        tab3_rb = findViewById(R.id.main_tab3_rb);

        fm = getSupportFragmentManager();
        fragmentOne = new MainOpenBill();
        fragmentTwo = new MainOrder();
        fragmentThree = new MainMine();

        changeFragment(0);


        presenter.homeData();
    }

    @Override
    protected void initListener() {
        super.initListener();
        main_rg.setOnCheckedChangeListener(listener);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }


    /**
     * 退出应用
     */
    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            ToastUtil.showToast("再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            killAll();
        }
    }

    @Override
    public void onClick(View v) {


    }

    private RadioGroup.OnCheckedChangeListener listener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            tab1_rb.setChecked(checkedId == tab1_rb.getId());
            tab2_rb.setChecked(checkedId == tab2_rb.getId());
            tab3_rb.setChecked(checkedId == tab3_rb.getId());
            if (checkedId == tab1_rb.getId()) {
                changeFragment(0);
            } else if (checkedId == tab2_rb.getId()) {
                changeFragment(1);
            } else if (checkedId == tab3_rb.getId()) {
                changeFragment(2);
            }
        }
    };

    private void changeFragment(int type) {
        switch (type) {
            case 0:
                FragmentUtils.fragmentUse(R.id.main_fl, fragmentOne, fm);
                mainTitle.setTitleText("开单");

                break;
            case 1:
                FragmentUtils.fragmentUse(R.id.main_fl, fragmentTwo, fm);
                mainTitle.setTitleText("订单");
                break;
            case 2:
                FragmentUtils.fragmentUse(R.id.main_fl, fragmentThree, fm);
                mainTitle.setTitleText("我的");
                break;
        }
    }
}
